//
//  DateHelper.m
//  CarNetwork_Shipper
//
//  Created by bubo on 15/10/29.
//  Copyright © 2015年 turingEdu. All rights reserved.
//

#import "DateHelper.h"

@implementation DateHelper

/**
 *  返回YYYYMMddHHmmss格式的时间戳
 *
 *  @return 字符串格式
 */
+(NSString *)returnDateStr{
    
    NSDate *NowTime=[NSDate date];
    NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
    [dateformat setDateFormat:@"YYYYMMddHHmmss"];
    NSString *dateStr = [NSString stringWithFormat:@"%@",[dateformat stringFromDate: NowTime]];
    return dateStr;
}

/**
 *  转换日期 今天、昨天
 *
 *  @param dateStr 字符串日期
 *
 *  @return 返回格式化后的字符串
 */
+(NSString *)ConvertToDate:(NSString*)dateStr{
    
    //今天
    NSDate *today = [NSDate date];
    //昨天
    NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-86400];
    
    //截取今天日期
    NSString * todayString = [[today description] substringToIndex:10];
    //截取昨天日期
    NSString * yesterdayString = [[yesterday description] substringToIndex:10];
    //传入日期
    NSString * refDateString = [[dateStr description] substringToIndex:10];
    
    if ([refDateString isEqualToString:todayString]){
        return @"今 天";
        
    }else if ([refDateString isEqualToString:yesterdayString]){
        return @"昨 天";
        
    }else{
        NSRange rang={5,5};
        dateStr=[dateStr substringWithRange:rang];
        dateStr=[dateStr stringByReplacingOccurrencesOfString:@"-" withString:@"月"];
        dateStr=[dateStr stringByReplacingOccurrencesOfString:@"0" withString:@""];
        return dateStr;
    }
}

/**
 *  计算指定时间与当前的时间差
 *
 *  @param theDate 某一指定时间
 *
 *  @return 多少(秒or分or天or月or年)+前 (比如，3天前、10分钟前)
 */
+(NSString *) compareCurrentTime:(NSString*)theDate{
    
    //字符串转为日期
    NSDateFormatter *formate=[[NSDateFormatter alloc]init];
    [formate setTimeZone:[NSTimeZone systemTimeZone]];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *compareDate = [formate dateFromString:theDate];
    
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60){
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
        
    }else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    return  result;
}

/**
 *  返回自定义格式化后的当前日期
 *
 *  @param formatStr 需要格式化的字符串
 *
 *  @return 格式化的结果
 */
+(NSString *)returnCustomDateStr:(NSString*)formatStr{
    
    NSDate *NowTime=[NSDate date];
    NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
    [dateformat setDateFormat:formatStr];
    NSString *dateStr = [NSString stringWithFormat:@"%@",[dateformat stringFromDate: NowTime]];
    return dateStr;
}

/**
 *  比较俩个时间大小
 *
 *  @param oneDay     第一个时间
 *  @param anotherDay 第二个时间
 *
 *  @return 返回结果 1:第一个时间大于第二个时间 -1:第一个时间小于第二个时间
 */
+(NSInteger)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    //NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        //第一个时间大于第二个时间
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        //第一个时间小于第二个时间
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
}

/**
 *  计算某一天距离现在多少天
 *
 *  @param theDate 日期格式
 *
 *  @return 返回天数
 */
+(NSInteger)intervalSinceNow:(NSString*)theDate{
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy/MM/dd"];
    NSDate *fromdate=[format dateFromString:theDate];
    NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
    NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
    NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];
    //NSLog(@"fromdate=%@",fromDate);
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    //NSLog(@"enddate=%@",localeDate);
    NSDateComponents *components = [gregorian components:unitFlags fromDate:fromDate toDate:localeDate options:0];
    NSInteger days = [components day];//年[components year] //月 [components month];
    //NSLog(@"days=%ld",(long)days);
    return days;
}

/**
 *  生成当天的某个点（返回的是伦敦时间，可直接与当前时间[NSDate date]比较）
 *
 *  @param hour hour 如hour为“8”，就是上午8:00（本地时间）
 *
 *  @return 返回日期
 */
+(NSDate *)getCustomDateWithHour:(NSInteger)hour{
    
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
    
    //设置当天的某个点
    NSDateComponents *resultComps = [[NSDateComponents alloc] init];
    [resultComps setYear:[currentComps year]];
    [resultComps setMonth:[currentComps month]];
    [resultComps setDay:[currentComps day]];
    [resultComps setHour:hour];
    
    NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    return [resultCalendar dateFromComponents:resultComps];
}

/**
 *  根据指定的日期格式化传入字符串，返回NSDate
 *
 *  @param dateStr   日期字符串
 *  @param formatStr 格式化格式
 *
 *  @return 返回格式化后的NSDater日期
 */
+(NSDate *)returnCustomDateWithDateStr:(NSString*)dateStr formatStr:(NSString*)formatStr{
    
    NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
    [dateformat setDateFormat:formatStr];
    NSDate *tempDate = [dateformat dateFromString:dateStr];
    return tempDate;
}

@end
